# Load store locations and subway stations
stores <- read.csv("stores_farmer_market.csv") |> # Update the file path if needed
filter(!is.na(latitude) & !is.na(longitude))
subways <- read.csv("subway.csv") # Update the file path if needed
# Convert stores and subways data into sf objects
stores_sf <- st_as_sf(stores, coords = c("longitude", "latitude"), crs = 4326)
subways_sf <- st_as_sf(subways, coords = c("longitude", "latitude"), crs = 4326)
# Buffer subway stations by 400 meters
subway_buffers <- st_buffer(subways_sf, dist = 400) # 400 meters buffer
# Find stores within the 400 meters buffer
stores_within_buffer <- st_intersection(stores_sf, subway_buffers)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# Create the leaflet map
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
# Add subway stations
addCircleMarkers(data = subways_sf, color = "red", radius = 5, popup = ~as.character(subways$station_name)) %>%
# Add stores within 400m radius
addCircleMarkers(data = stores_within_buffer, color = "green", radius = 3, popup = ~stores$healthy_store_market, label = ~stores$healthy_store_market) %>%
# Add 400m buffer zones around subway stations
addPolygons(data = subway_buffers, fillColor = "yellow", fillOpacity = 0.1, color = "gray", weight = 0.5) %>%
# Add legend
addLegend(position = "bottomright",
colors = c("red", "green", "yellow"),
labels = c("Subway Stations", "Stores", "400 meters"))